package com.bingbaihanji.cloud.controller;

import com.bingbaihanji.cloud.entities.PayDTO;
import com.bingbaihanji.cloud.resp.ResultData;
import jakarta.annotation.Resource;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author 冰白寒祭
 * @date 2024-03-07 14:17:22
 * @description //TODO
 */
@RestController
public class OrderController {
    //    public static final String paymentSry_URL = "http://localhost:8001";
    public static final String PaymentSrv_URL = "http://cloud-payment-service";

    @Resource
    private RestTemplate restTemplate;

    @Resource
    private DiscoveryClient discoveryClient;

    @GetMapping("/consumer/pay/add")
    public ResultData addOrder(PayDTO payDTO) {
        return restTemplate.postForObject(PaymentSrv_URL + "/pay/add", payDTO, ResultData.class);
    }

    @GetMapping("/consumer/pay/del/{id}")
//    public void deletePay(@PathVariable("id") Integer id) {
//        restTemplate.delete(paymentSry_URL + "/pay/del/" + id, ResultData.class, id);
//    }
    public ResponseEntity<ResultData> deletePay(@PathVariable("id") Integer id) {
        ResponseEntity<ResultData> exchange = restTemplate.exchange(PaymentSrv_URL + "/pay/del/" + id,
                HttpMethod.DELETE, null, ResultData.class, id);
        return exchange;

    }

    @GetMapping("/consumer/pay/update")
    public ResponseEntity<ResultData> updatePay(PayDTO payDTO) {
        HttpEntity<PayDTO> entity = new HttpEntity<>(payDTO);
        ResponseEntity<ResultData> exchange = restTemplate.exchange(PaymentSrv_URL + "/pay/update",
                HttpMethod.PUT, entity, ResultData.class);
        return exchange;
    }

    @GetMapping("/consumer/pay/get/{id}")
    public ResultData getPayInfo(@PathVariable("id") Integer id) {
        return restTemplate.getForObject(PaymentSrv_URL + "/pay/get/" + id, ResultData.class, id);
    }

    @GetMapping(value = "/consumer/pay/get/info")
    private String getInfoByConsul() {
        return restTemplate.getForObject(PaymentSrv_URL + "/pay/get/info", String.class);
    }


    @GetMapping("/consumer/discovery")
    public String discovery() {
        List<String> services = discoveryClient.getServices();
        for (String element : services) {
            System.out.println(element);
        }

        System.out.println("===================================");

        List<ServiceInstance> instances = discoveryClient.getInstances("cloud-payment-service");
        for (ServiceInstance element : instances) {
            System.out.println(element.getServiceId() + "\t" + element.getHost() + "\t" + element.getPort() + "\t" + element.getUri());
        }
        return instances.get(0).getServiceId() + ":" + instances.get(0).getPort();
    }
}
